﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mani.Common.Security;
using RastinArgham.CRM.Business;

namespace RastinArgham.CRM.Web
{
    public partial class robot_reports : System.Web.UI.Page
    {
        public ISecurityManager securityManager { get; set; }
        public CRMRepository crmRepository { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (securityManager == null)
            {
                securityManager = new SecurityManager();
                securityManager.authProvider = new WebAuthorizationProvider();
            }

            if (crmRepository == null)
                crmRepository = new CRMRepository();


            if (!securityManager.IsActionPermitted("robot_reports"))
                Response.Redirect("~/access_denied.aspx");

            if (!Page.IsPostBack)
            {
                LoadCities();
                FindRobotReports();
            }
        }

        private void LoadCities()
        {
            SecurityManager securityManager = new SecurityManager();
            securityManager.authProvider = new WebAuthorizationProvider();
            CRMRepository crmRepository = new CRMRepository();
            ddlCities.DataSource = crmRepository.FindCitiesByUser(securityManager.authProvider.GetCurrentUser().UserId);
            ddlCities.DataBind();
        }

        private void FindRobotReports()
        {
            List<RobotReport> reports = crmRepository.GetRobotReports(txtUsername.Text, ddlAction.SelectedValue, Convert.ToInt32(ddlStatus.SelectedValue));

            if (!securityManager.IsActionPermitted("robot_reports_all"))
            {
                List<int> orderIds = reports.Where(r => r.OrderId.HasValue).Select(r => r.OrderId.Value).ToList();
                Guid myId = securityManager.authProvider.GetCurrentUser().UserId;
                List<Guid> myAccountManagers = crmRepository.FindMyAccountManagers(myId);
                List<int> contacts = new List<int>();

                if (ddlCities.SelectedValue == "-1")
                    contacts = crmRepository.SearchContactIds(myAccountManagers, myId);
                else
                    contacts = crmRepository.SearchContactIds(myAccountManagers, myId, Convert.ToInt32(ddlCities.SelectedValue));

                List<int> AllowedOrders = crmRepository.SearchServiceOrdersByContacts(contacts, orderIds);
                reports = reports.Where(r => AllowedOrders.Contains(r.OrderId.Value)).ToList();
            }
            else
            {
                if (ddlCities.SelectedValue != "-1")
                    reports = crmRepository.FilterReportsByCity(Convert.ToInt32(ddlCities.SelectedValue), reports); 
            }

            gvRobotReports.DataSource = reports;
            gvRobotReports.DataBind();
        }

        public string GetReport(object report, bool success)
        {

            if (report == null)
                return "";

            if (!success && report.ToString().Length > 70)
                return "خطا ";
            else
                return report.ToString();
        }

        protected void gvRobotReports_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRobotReports.PageIndex = e.NewPageIndex;
            FindRobotReports();
        }

        protected void gvRobotReports_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int reportId = Convert.ToInt32(gvRobotReports.DataKeys[e.NewSelectedIndex].Value);
            RobotReport report = crmRepository.GetRobotReportById(reportId);

            if (report == null)
                return;

            divDetails.Visible = true;
            lblReportDetail.Text = report.Report;
        }

        public string GetUsername(object username)
        {
            if (username == null)
                return "تعریف نشده است";
            else
                return username.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FindRobotReports();
        }
    }
}